<%
    $not_concluded     = $model->qtd_atividades_por_conclusao[ConclusionTypes::NOT_CONCLUDED];
    $before_alert      = $model->qtd_atividades_por_conclusao[ConclusionTypes::CONCLUDED_BEFORE_ALERT];
    $before_conclusion = $model->qtd_atividades_por_conclusao[ConclusionTypes::CONCLUDED_BEFORE_CONCLUSION];
    $before_tolerance  = $model->qtd_atividades_por_conclusao[ConclusionTypes::CONCLUDED_BEFORE_TOLERANCE];
    $after_tolerance   = $model->qtd_atividades_por_conclusao[ConclusionTypes::CONCLUDED_AFTER_TOLERANCE];

    $concluded = $before_alert + $before_conclusion + $before_tolerance + $after_tolerance;
    $totalActivities = $not_concluded + $concluded;

    $baseUrl = Yii::app()->baseUrl;
    $clientScript = Yii::app()->getClientScript();

    $clientScript->registerCoreScript('jquery.ui');
    $clientScript->registerScriptFile($baseUrl . '/js/chart.min.js');
    $clientScript->registerCssFile($baseUrl . '/css/jquery-ui-1.10.1.custom.min.css');

    $clientScript->registerScript('statistics_update_datepicker_and_charts', "
        //Code for update statistics according to the filters
        $('#estatistica-form input, #estatistica-form select').change(function() {
            $('#estatistica-form').submit();
        });

        //Code for set up datepicker
        $('#data_inicial, #data_final').datepicker({
            dateFormat: 'dd/mm/yy',
            dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'],
            dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
            dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
            monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
            monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'],
            nextText: 'Próximo Mês',
            prevText: 'Mês Anterior',
            minDate: '-6M',
            maxDate: 0
        });

        //Code for set up charts
        var nao_concluida     = " . $not_concluded     . ";
        var antes_alerta      = " . $before_alert      . ";
        var antes_conclusao   = " . $before_conclusion . ";
        var antes_tolerancia  = " . $before_tolerance  . ";
        var depois_tolerancia = " . $after_tolerance   . ";
        var total_concluida   = " . $concluded . ";

        var data = [
        {
            value : total_concluida,
            color : '#69D2E7' //azul
        },
        {
            value : nao_concluida,
            color : '#F38630' //laranja
        }];

        var options = {
            //Boolean - Whether we should animate the chart
            animation : true,//false,
            //Number - Amount of animation steps
            animationSteps : 100,
            //String - Animation easing effect
            animationEasing : 'linear'
        };

        if(nao_concluida != 0 || total_concluida != 0) {
            var ctx = $('#chart1').get(0).getContext('2d');
            var myNewChart = new Chart(ctx).Pie(data, options);
        }

        data = [
        {
            value : depois_tolerancia,
            color : '#e06666' //vermelho
        },
        {
            value : antes_tolerancia,
            color : '#ff0'    //amarelo
        },
        {
            value : antes_conclusao,
            color : '#93c47d' //verde
        },
        {
            value: antes_alerta,
            color:'#eee'      //cinza
        }];

        if(total_concluida != 0) {
            var ctx = $('#chart2').get(0).getContext('2d');
            var myNewChart = new Chart(ctx).Pie(data, options);
        }
    ");

    $form = $this->beginWidget('CActiveForm', array(
        'action' => Yii::app()->createUrl($this->route),
        'method' => 'get',
        'htmlOptions' => array('class' => 'grid-view', 'id' => 'estatistica-form')
    ));
%>

    <table class="items">
        <thead>
            <tr>
                <th><%= $form->label($model, 'data_inicial'); %></th>
                <th><%= $form->label($model, 'data_final'); %></th>
                <th><%= $form->label($model, 'centro_de_operacao'); %></th>
                <th><%= $form->label($model, 'orgao'); %></th>
            </tr>
            <tr class="filters">
                <td><%= $form->textField($model, 'data_inicial', array('id' => 'data_inicial')); %></td>
                <td><%= $form->textField($model, 'data_final', array('id' => 'data_final')); %></td>
                <td>
                    <%= $form->dropDownList(
                            $model,
                            'centro_de_operacao',
                            CHtml::listData($centros_de_operacao_e_orgaos, 'centro_de_operacao', 'centro_de_operacao'),
                            array('prompt' => 'Todos')
                        );
                    %>
                </td>
                <td>
                    <%= $form->dropDownList(
                            $model,
                            'orgao',
                            CHtml::listData($centros_de_operacao_e_orgaos, 'orgao', 'orgao'),
                            array('prompt' => 'Todos')
                        );
                    %>
                </td>
            </tr>
        </thead>
        <% if($totalActivities != 0) { %>
            <tbody>
                <tr>
                    <td colspan="2">
                        <p style="font-weight: bold;text-align: center">Todas as atividades</p>
                        <table style="width: 100%">
                            <tr style="color: #F38630">
                                <td><%= round(100 * $not_concluded/$totalActivities, 1) . '% não concluídas'; %></td>
                                <td rowspan="3"><canvas id="chart1" width="250px" height="250px"></canvas></td>
                            </tr>
                            <tr style="color: #69D2E7">
                                <td><%= round(100 * $concluded/$totalActivities, 1) . '% concluídas'; %></td>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                        </table>
                        <p style="font-weight: bold;text-align: center"><%= sprintf('Total: %d atividade%s', $totalActivities, $totalActivities == 1 ? '' : 's'); %></p>
                    </td>
                    <td colspan="2">
                        <% if($concluded != 0): %>
                            <p style="font-weight: bold;text-align: center">Atividades concluídas</p>
                            <table style="width: 100%">
                                <tr style="color: #eee">
                                    <td><%= round(100 * $before_alert/$concluded, 1) . '% em preparação'; %></td>
                                    <td rowspan="5"><canvas id="chart2" width="250px" height="250px"></canvas></td>
                                </tr>
                                <tr style="color: #93c47d">
                                    <td><%= round(100 * $before_conclusion/$concluded, 1) . '% em execução'; %></td>
                                </tr>
                                <tr style="color: #ff0">
                                    <td><%= round(100 * $before_tolerance/$concluded, 1) . '% em alerta'; %></td>
                                </tr>
                                <tr style="color: #e06666">
                                    <td><%= round(100 * $after_tolerance/$concluded, 1) . '% em atraso'; %></td>
                                </tr>
                                <tr>
                                    <td></td>
                                </tr>
                            </table>
                            <p style="font-weight: bold;text-align: center"><%= sprintf('Total: %d atividade%s', $concluded, $concluded == 1 ? '' : 's'); %></p>
                        <% endif; %>
                    </td>
                </tr>
            </tbody>
        <% } else { %>
            <td colspan="4">Não há atividades cadastradas nas condições selecionadas.</td>
        <% } %>
    </table>

<%
    if(Yii::app()->user->hasFlash('message'))
        Yii::app()->clientScript->registerScript('message',
            sprintf('showMessage("%s", true);', Yii::app()->user->getFlash('message')));

    $this->endWidget();
%>